//
//  DecorateSonViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/8/31.
//  Copyright © 2016年 LST. All rights reserved.
//  Decorate 详情

#import "DecorateSonViewController.h"
#import "DecorateSonTableViewCell.h"
#import "DecorateSonHeaderView.h"
#import "AFNetworking.h"
#import "SonModel.h"
#import "SonUser.h"
#import "SonUserImage.h"
#import "TaskEnd.h"
#import "Space.h"
#import "Img.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "FreeDesignWebViewController.h"
#import "DecorateHeaderTableViewCell.h"
#import "DecorationSonHeaderTableViewCell.h"
#import "SonImageViewController.h"
#import "UMSocial.h"
#import "NetworkHandler.h"
#import "LoginLeftViewController.h"
#import "OneTableView.h"
@interface DecorateSonViewController ()<UITableViewDataSource, UITableViewDelegate, PressTableViewCellDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)SonModel *sonModel;
@property (nonatomic, strong)DecorateSonHeaderView *headerView;
@property (nonatomic, strong)UILabel *leftLabel;
@property (nonatomic, strong)UILabel *rightLabel;
@property (nonatomic, strong)UIButton *leftBtn;
@property (nonatomic, strong)UIButton *rightBtn;
@property (nonatomic, strong)UIButton *bigBtn;
@end

@implementation DecorateSonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(clickLeft:)];
    [self handData];
    [self creatTableView];
    [self creatSubView];
}
- (void)handData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
    [set addObject:@"text/plain"];
    manager.responseSerializer.acceptableContentTypes = set;

    
    
    [manager GET:[NSString stringWithFormat:@"http://api.guju.com.cn/v2/project/%@/v2?userId=517194", _urlId] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.sonModel = [SonModel modelWithDic:dic];
        [_tableView reloadData];
        // view 赋值
        _headerView.sonModel = _sonModel;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
}


- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 50)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"DecorateSonTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    [_tableView registerNib:[UINib nibWithNibName:@"DecorationSonHeaderTableViewCell" bundle:[NSBundle mainBundle]]forCellReuseIdentifier:@"HeaderCell"];
    self.headerView = [[DecorateSonHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2)];
    _tableView.tableHeaderView = _headerView;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10, 20, 50, 35);
    [btn setImage:[UIImage imageNamed:@"返回, 实心"] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(clickLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    btn2.frame = CGRectMake(self.view.frame.size.width - 60, 20, 50, 35);
    [btn2 addTarget:self action:@selector(clickRightBtn:) forControlEvents:UIControlEventTouchUpInside];
}
// 免费设计按钮
- (void)creatSubView {
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.width.mas_equalTo(self.view);
        make.height.mas_equalTo (50);
    }];
    self.leftLabel = [[UILabel alloc] init];
    _leftLabel.text = @"36";
    [self.view addSubview:_leftLabel];
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.bottom.mas_equalTo(bottomView);
        make.width.mas_equalTo(self.view.bounds.size.width / 8);
        make.left.mas_equalTo (self.view.bounds.size.width / 8 );
    }];
    _rightLabel = [[UILabel alloc] init];
    _rightLabel.text = @"15";
    [self.view addSubview:_rightLabel];
    [_rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.bottom.mas_equalTo(bottomView);
        make.width.mas_equalTo(self.view.bounds.size.width / 8);
        make.left.mas_equalTo (self.view.bounds.size.width / 3);
    }];
    _bigBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_bigBtn setTitle:@"免费设计" forState:UIControlStateNormal];
    _bigBtn.backgroundColor = [UIColor colorWithRed:0.15 green:0.33 blue:0.59 alpha:1.00];
    [self.view addSubview:_bigBtn];
    [_bigBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_bigBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo (self.view.bounds.size.width / 2);
        make.height.bottom.mas_equalTo(bottomView);
        make.left.mas_equalTo(self.view.bounds.size.width / 2);
    }];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setImage:[UIImage imageNamed:@"小心"] forState:UIControlStateNormal];
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(clickCollectBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (20);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo (25);
        make.centerY.mas_equalTo (_leftLabel);
    }];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setImage:[UIImage imageNamed:@"小对话"] forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.bottom.mas_equalTo (btn1);
        make.left.mas_equalTo (self.view.bounds.size.width / 4);
    }];
}
//15940036070
- (void)handCollectionData {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [user objectForKey:@"dic"];
    NSString *secret = [dic objectForKey:@"secret"];
    NSString *key = [dic objectForKey:@"key"];
    NSString *userID = _urlId;
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval timeInterVal = [date timeIntervalSince1970];
    // 时间戳
    NSString *strTimer = [NSString stringWithFormat:@"%.0f", timeInterVal];
    NSString *strUrl = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/project/%@/like?type=1", userID];
    NSString *strBody = [NSString stringWithFormat:@"secret=%@&datestamp=%@&key=%@", secret, strTimer, key];
    [NetworkHandler getDataByUrlString:strUrl BodyString:strBody WithDataBlock:^(id result) {
        NSLog(@"收藏请求%@", result);
    }];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.bounds.size.height / 3;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _sonModel.taskEnd.space.count + 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
    } else {
        SonImageViewController *sonImageVC = [[SonImageViewController alloc] init];
        [self.navigationController pushViewController:sonImageVC animated:YES];
    }
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    } else {
        return _sonModel.taskEnd.space[section - 1].name;
    }}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return _sonModel.taskEnd.space[section - 1].img.count;
    }}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        DecorationSonHeaderTableViewCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"HeaderCell"];
        headerCell.taskEnd = _sonModel.taskEnd;
        return headerCell;
    } else {
        DecorateSonTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
        cell.img = _sonModel.taskEnd.space[indexPath.section - 1].img[indexPath.row];
        
        return cell;
    }}
- (void)clickLeft:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)clickLeftBtn:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden = NO;
    
}

// 分享
- (void)clickRightBtn:(UIButton *)sender {
    NSURL *url = [NSURL URLWithString:self.urlStr];
    NSString *string = [NSString stringWithFormat:@"%@", url];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55e85cb9e0f55af6ba0061c1"
                                      shareText:string
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToTencent,UMShareToRenren,UMShareToSina,UMShareToQzone,UMShareToEmail,UMShareToFacebook,UMShareToDouban,UMShareToSms,UMShareToQQ,nil]
                                       delegate:nil];
}
- (void)clickBtn:(UIButton *)sender {
    FreeDesignWebViewController *freeDesignWebVC = [[FreeDesignWebViewController alloc] init];
    [self.navigationController pushViewController:freeDesignWebVC animated:YES];
}
- (void)clickCollectBtn:(UIButton *)sender {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [user objectForKey:@"dic"];
    NSString *userName = dic[@"user"][@"userName"];
    if (userName.length > 0) {
        [self handCollectionData];
        [_tableView reloadData];
    } else {
        LoginLeftViewController *VC = [[LoginLeftViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
        
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
